package cn.com.qiter.minioservice.service.impl;

import cn.com.qiter.common.Result;
import cn.com.qiter.minioservice.service.MinioService;
import cn.com.qiter.minioservice.utils.ConstantPropertiesUtils;
import cn.com.qiter.minioservice.utils.MinioUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
@Log4j2
public class MinioServiceImpl implements MinioService {

    @Autowired
    MinioUtil minioUtil;
    @Override
    public Result putObject(String bucketName, MultipartFile multipartFile, String filename) {
        String simpleUUID = IdUtil.simpleUUID();
        String type = StrUtil.subAfter(multipartFile.getOriginalFilename(),".",true);
        String filenames = simpleUUID+"."+type;
        log.error(filenames);
        try{
            minioUtil.putObject(bucketName,multipartFile,filenames);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error();
        }
        return Result.ok().data("filename", ConstantPropertiesUtils.END_POIND+"/"+bucketName+"/"+filenames);
    }

    @Override
    public Result getObjectUrl(String bucketName, String objectName) {
        String url = minioUtil.getObjectUrl(bucketName,objectName);
        return Result.ok().data("url",url);
    }

    @Override
    public Result presignedGetObject(String bucketName, String objectName) {
        String url = minioUtil.presignedGetObject(bucketName,objectName,60 * 60 * 24 * 5);
        return Result.ok().data("url",url);
    }
}
